package com.inossem.warehousestorage_inossem.adapter.inStorage.chuangyou;

import android.content.Context;
import android.content.Intent;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;
import com.inossem.utils.base.Utils;
import com.inossem.utils.listener.NoDoubleClickListener;
import com.inossem.utils.log.LogUtils;
import com.inossem.warehousestorage_inossem.R;
import com.inossem.warehousestorage_inossem.activity.change_storage.extra.ChangeStorageExtra;
import com.inossem.warehousestorage_inossem.activity.in_storage.chuangyou.CYInStorageProductionListActivity;
import com.inossem.warehousestorage_inossem.activity.in_storage.chuangyou.CYInStorageProductionOverViewActivity;
import com.inossem.warehousestorage_inossem.activity.in_storage.chuangyou.CYInStorageShoppingOverViewActivity;
import com.inossem.warehousestorage_inossem.activity.in_storage.extra.InStorageExtra;
import com.inossem.warehousestorage_inossem.bean.inStorage.chuangyou.CYInStorageProductionListBean;
import com.inossem.warehousestorage_inossem.bean.inventory.InventorySupplierListBean;


import java.util.ArrayList;
import java.util.List;

/**
 * TODO
 *
 * @author 王斯宇
 * @time on 2018/3/26 19:49
 * @email siyu.wang@inossem.com
 */
public class CYProductionInStorageListAdapter extends BaseAdapter {

    private Context mContext;
    private List<CYInStorageProductionListBean.OrderBean> list = new ArrayList<>();
    private LayoutInflater inflater;
    private String code;
    private CYInStorageProductionListActivity.CYInStorageProductionListener listener;
    private EditText codeEditText;
    private String mCondition , mChoose;

    public CYProductionInStorageListAdapter(Context context , String condition ,String choose , List<CYInStorageProductionListBean.OrderBean> list , CYInStorageProductionListActivity.CYInStorageProductionListener listener) {
        this.mContext = context;
        this.inflater = LayoutInflater.from(context);
        this.mCondition = condition;
        this.list = list;
        this.listener = listener;
        this.mChoose = choose;
    }

    @Override
    public int getCount() {
        return list.size() + 1;
    }

    @Override
    public Object getItem(int position) {
        return list.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        if (position == 0) {
            convertView = inflater.inflate(R.layout.cy_view_in_storage_production_list_first_item, null);
            codeEditText = (EditText) convertView.findViewById(R.id.code);
            ImageView search = (ImageView) convertView.findViewById(R.id.search);
            codeEditText.setHint(R.string.cy_activity_in_storage_production_list_production_of_put_in_storage_please_input_order_number);
            TextView productionCompleteTextView = (TextView) convertView.findViewById(R.id.productionCompleteTextView);
            View productionComplete = convertView.findViewById(R.id.productionComplete);
            TextView noInStorageTextView = (TextView) convertView.findViewById(R.id.noInStorageTextView);
            View noInStorage = convertView.findViewById(R.id.noInStorage);
            if (mChoose.equals(CYInStorageProductionListActivity.PRODUCTION_COMPLETE)) {
                productionCompleteTextView.setTextColor(mContext.getResources().getColor(R.color.activity_receive_delivery_overview_font_color));
                productionComplete.setVisibility(View.VISIBLE);
                noInStorageTextView.setTextColor(mContext.getResources().getColor(R.color.common_font_item_name));
                noInStorage.setVisibility(View.GONE);
            }else if (mChoose.equals(CYInStorageProductionListActivity.NO_INSTORAGE)) {
                productionCompleteTextView.setTextColor(mContext.getResources().getColor(R.color.common_font_item_name));
                productionComplete.setVisibility(View.GONE);
                noInStorageTextView.setTextColor(mContext.getResources().getColor(R.color.activity_receive_delivery_overview_font_color));
                noInStorage.setVisibility(View.VISIBLE);
            }
            productionCompleteTextView.setOnClickListener(new NoDoubleClickListener() {
                @Override
                public void onNoDoubleClick(View v) {
                    productionCompleteTextView.setTextColor(mContext.getResources().getColor(R.color.activity_receive_delivery_overview_font_color));
                    productionComplete.setVisibility(View.VISIBLE);
                    noInStorageTextView.setTextColor(mContext.getResources().getColor(R.color.common_font_item_name));
                    noInStorage.setVisibility(View.GONE);
                    listener.onProductionCompleteClickListener();
                }
            });
            noInStorageTextView.setOnClickListener(new NoDoubleClickListener() {
                @Override
                public void onNoDoubleClick(View v) {
                    productionCompleteTextView.setTextColor(mContext.getResources().getColor(R.color.common_font_item_name));
                    productionComplete.setVisibility(View.GONE);
                    noInStorageTextView.setTextColor(mContext.getResources().getColor(R.color.activity_receive_delivery_overview_font_color));
                    noInStorage.setVisibility(View.VISIBLE);
                    listener.onNoInStorageClickListener();
                }
            });
            if(!TextUtils.isEmpty(mCondition)){
                codeEditText.setText(mCondition);
            }
            search.setOnClickListener(new NoDoubleClickListener() {
                @Override
                public void onNoDoubleClick(View v) {
                    Utils.closeSoftKeyboard(mContext, v);
                    CYProductionInStorageListAdapter.this.mCondition = codeEditText.getText().toString();
//                    getData(mContext, CYProductionInStorageListAdapter.this.code);
                    listener.onNoDoubleClickListener(TextUtils.isEmpty(codeEditText.getText().toString())? "" : codeEditText.getText().toString());
                }
            });
        } else {
            CYInStorageProductionListBean.OrderBean materielItem = list.get(position - 1);
            ViewHolder viewHolder;
            if (convertView == null || (convertView != null && convertView.getTag() == null)) {
                convertView = inflater.inflate(R.layout.cy_view_production_in_storage_list_item, null);
                viewHolder = new ViewHolder();
                //初始化控件
                initView(viewHolder, convertView);
                convertView.setTag(viewHolder);
            } else {
                viewHolder = (ViewHolder) convertView.getTag();
            }
            //初始化数据
            initData(mContext, viewHolder, convertView, materielItem);
        }
        return convertView;
    }

    private void initView(ViewHolder viewHolder, View view) {
        viewHolder.orderNumber = (TextView) view.findViewById(R.id.productionInStorageOrderNumber);
//        viewHolder.status = (TextView) view.findViewById(R.id.status);
//        viewHolder.materialDescribe = (TextView) view.findViewById(R.id.materialDescribe);
        viewHolder.creatTime = (TextView) view.findViewById(R.id.creatTime);
    }

    private void initData(Context context, ViewHolder viewHolder, View view, CYInStorageProductionListBean.OrderBean item) {
        viewHolder.orderNumber.setText(item.getStock_input_code());
//        viewHolder.status.setText("未过账");
//        viewHolder.materialDescribe.setText("碳素结构花纹钢板δ=6mm Q235B GB/T3277碳素结构花纹钢板δ=6mm Q235B GB/T3277");
        viewHolder.creatTime.setText(item.getPurchase_order_code());
        view.setOnClickListener(new NoDoubleClickListener() {
            @Override
            public void onNoDoubleClick(View v) {
                Intent intent = new Intent();
                intent.setClass(mContext , CYInStorageProductionOverViewActivity.class);
                intent.putExtra(InStorageExtra.STOCK_INPUT_ID, item.getStock_input_id());
                intent.putExtra(InStorageExtra.STATUS, item.getStatus());
                mContext.startActivity(intent);
//
            }
        });
    }


    private class ViewHolder {
        private TextView orderNumber, status , materialDescribe , creatTime;
    }

}
